package com.example.his.api.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.common.R;
import com.example.his.api.db.dao.GoodsSnapshotDao;
import com.example.his.api.front.controller.form.SearchGoodsSnapshotByIdForm;
import com.example.his.api.front.controller.form.goods.SearchGoodsListByPageForm;
import com.example.his.api.front.controller.form.goods.SearchIndexGoodsByPartForm;
import com.example.his.api.front.service.GoodsService;
import com.example.his.api.front.controller.form.goods.SearchGoodsByIdForm;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController("FrontGoodsController")
@RequestMapping("/front/goods")
public class GoodsController {
  @Resource
  private GoodsService goodsService;
  
  
  @PostMapping("/searchById")
  public R searchById(@Valid @RequestBody SearchGoodsByIdForm form) {
    HashMap map = goodsService.searchById(form.getId());
    return R.ok().put("result", map);
  }
  
  //  查询某个商品中销量前四名的商品记录
  @PostMapping("/searchIndexGoodsByPart")
  public R searchIndexGoodsByPart(@RequestBody @Valid SearchIndexGoodsByPartForm form) {
    HashMap map = goodsService.searchIndexGoodsByPart(form.getPartIds());
    
    return R.ok().put("result", map);
  }
  
  //  业务端根据条件查询商品列表
  @PostMapping("/searchListByPage")
  public R searchListByPage(@RequestBody @Valid SearchGoodsListByPageForm form) {
    
    int page = form.getPage();
    int length = form.getLength();
    int start = (page - 1) * length;
    Map param = BeanUtil.beanToMap(form);
    param.put("start", start);
    PageUtils pageUtils = goodsService.searchListByPage(param);
    return R.ok().put("result", pageUtils);
  }
  
  
  @PostMapping("/searchSnapshotForMis")
  @SaCheckLogin
  public R searchSnapshotForMis(@RequestBody @Valid SearchGoodsSnapshotByIdForm form) {
    HashMap map = goodsService.searchSnapshotById(form.getSnapshotId(), null);
    return R.ok().put("result", map);
  }
  
}
